#!/usr/bin/env python

from _dbus_glib_bindings import DBusGMainLoop
import os
import sys
import dbus

if __name__ == '__main__':
    if '-d' in sys.argv:
        print '---compile gui---'
        os.system('pyuic4 -o ui/mainwindow_gen.py ui/main.ui')

        print '---compile resources---'
        os.system('pyrcc4 -o ui/res_rc.py res/res.qrc')

        print '---update translations---'
        tr = ['ru_ru']

        for lang in tr:
            os.system('pylupdate4 *.py ui/*.py phonik/plugins/*.py -ts translations/phonik.' + lang + '.ts')
            os.system('lrelease translations/phonik.ru_ru.ts -qm translations/phonik.' + lang + '.qm')

    # check phonik.py existence
    DBusGMainLoop(set_as_default=True)
    bus = dbus.SessionBus()
    phonikService = None

    # get phonik.py dbus service
    try:
        phonikService = bus.get_object('com.phonik.PhonikInterface', '/org/frankhale/helloservice')
    except dbus.DBusException:
        pass

    # start service or bring it to front
    if not phonikService:
        from phonik.phonik import Phonik
        Phonik()
    else:
        phonikService.showHide()
        dbusArray = []
        for arg in sys.argv[1:]:
            arg = unicode(arg, 'utf-8')
            dbusArray.append(dbus.String(arg))

        array = dbus.Array(dbusArray, signature=dbus.Signature('s'))
        phonikService.parserArgs(array)
